package com.sky.controller.admin;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import nonapi.io.github.classgraph.scanspec.ScanSpec;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/admin/order")
@Slf4j
@Api(tags = "订单查看")
@RestController("AdminOrderController")
public class OrderController {

    @Autowired
    OrderService orderService;

    @GetMapping("/conditionSearch")
    @ApiOperation("订单分页查询")
    public Result<PageResult> conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO) {
        log.info("订单分页查询：{}", ordersPageQueryDTO);
        PageHelper.startPage(ordersPageQueryDTO.getPage(), ordersPageQueryDTO.getPageSize());

        PageResult pageResult = orderService.queryPage(ordersPageQueryDTO);

        return Result.success(pageResult);
    }

    @GetMapping("/statistics")
    @ApiOperation("订单数量查询")
    public Result<OrderStatisticsVO> getStatisticsVO() {
        OrderStatisticsVO statisticsVO = orderService.getStatistics();
        log.info("待配送数量：{}  配送中数量：{}  待接单数量：{}",
                statisticsVO.getConfirmed(), statisticsVO.getDeliveryInProgress(), statisticsVO.getToBeConfirmed());

        return Result.success(statisticsVO);
    }

    @GetMapping("/details/{id}")
    @ApiOperation("订单数量查询")
    public Result<OrderVO> getDetails(@PathVariable Long id) {
        log.info("查看id为：{} 的订单详细", id);
        OrderVO orderVO = orderService.getOrderDetail(id);

        return Result.success(orderVO);
    }

    @PutMapping("/confirm")
    @ApiOperation("接单")
    public Result<Object> confirm(@RequestBody OrdersConfirmDTO confirmDTO) {
        Long id = confirmDTO.getId();
        log.info("接单id -> 订单id：{}", id);
        orderService.confirm(id);

        return Result.success();
    }

    @PutMapping("/rejection")
    @ApiOperation("拒单")
    public Result<Object> reject(@RequestBody OrdersRejectionDTO rejectionDTO) {
        Long id = rejectionDTO.getId();
        String rejectionReason = rejectionDTO.getRejectionReason();
        log.info("拒单id -> 订单id：{} 原因：{}", id, rejectionReason);
        orderService.reject(id, rejectionReason);

        return Result.success();
    }

    @PutMapping("/cancel")
    @ApiOperation("取消订单")
    public Result<Object> cancel(@RequestBody OrdersCancelDTO cancelDTO) {
        Long id = cancelDTO.getId();
        String cancelReason = cancelDTO.getCancelReason();
        log.info("取消订单 -> 订单id为：{}", id);
        orderService.cancel(id, cancelReason);

        return Result.success();
    }

    @PutMapping("/delivery/{id}")
    @ApiOperation("派单")
    public Result<Object> delivery(@PathVariable Long id) {
        log.info("派单id -> 订单id：{}", id);
        orderService.delivery(id);

        return Result.success();
    }

    @PutMapping("/complete/{id}")
    @ApiOperation("派单")
    public Result<Object> complete(@PathVariable Long id) {
        log.info("完成订单 -> 订单id：{}", id);
        orderService.complete(id);

        return Result.success();
    }
}
